<?php

// 0. Create the data.
include("Data.php");
$data = (new Data())->getData();

// 1. The traditional recursive method to get tree.
$start_time = microtime(true);

$result = recursiveTree($data, 0);
function recursiveTree($data = [], $father_id = 0, $level = 0)
{
    static $list = [];
    static $level = 0;
    foreach ($data as $key => $value) {
        if ($value['pid'] == $father_id) {
            $level = $level + 1;
            $value['level'] = $level;
            $list[] = $value;
            recursiveTree($data, $value['code'], $level);
        }
    }
    $level--;
    return $list;
}

$end_time = microtime(true);
echo "\r\n" . ($end_time - $start_time) * 1000 . " ms\r\n";
// Use time 677ms - 697 ms


// 2. The improved level up to top search method to get tree.
$start_time = microtime(true);

include("Tree.php");
$option = [
    'node_name_key' => 'name',
    'father_name_key' => 'pid',
    'son_node_key' => 'code',
    'level_key' => 'level',
    'top_level_id' => 0,
];
$tree = new Tree($data, $option);
$result = $tree->getAllNodeTreePath();

$end_time = microtime(true);
echo "\r\n" . ($end_time - $start_time) * 1000 . " ms\r\n";
// Use time 8.67ms - 9.64 ms

// 3. Show the level tree.
foreach ($result as $v) {
    echo str_repeat('-', $v['level']), $v['name'],  "\r\n";
}
